var vm = new Vue({
    el: '#app',
    data: {
        exp_flag:false,
        exp_flag1:true,
        num:'',
        myChecked:"",
        host: host,
        show: false,
        show1: false,
        show2: false,
        show3: false,
        show4: false,
        hide:false,
        hide1:false,
        hide2:false,
        hide3:false,
        flag1:true,
        flag2:true,
        flag3:true,
        flag4:true,
        flag5:true,

        sophistication:'50',
        exp_id:"",
        sty:"",
        edit:"",
        click_flag:false,
        value:"",
        key:"",
        index:"",
        array_exp:[],
        array_edu:[],
        start_time:"",
        end_time:"",
        work_city:"",
        edu_technology :"",
        exp_technology:"",
        job_description:"",
        background:"",
        achievements:"",
        city:'',
        school_name:'',
        professional:'',
        company:'',
        profession:'',
        nick_name:'',
        web_site:'',
        email:'',
        mobile:'',
        password:'',
        real_name:'',
        gender:'',
        birthday:'' ,
        technology:'',
        hometown:'',
        exp:'',
        education:'',
        introduce:'',
        avatar_url:'',
        exp_company:"",
        exp_profession:"",
        edu_school:"",
        edu_professional:"",
        token: sessionStorage.token || localStorage.token,
        user_id: sessionStorage.user_id || localStorage.user_id,
        username:sessionStorage.username|| localStorage.username,
    },
    mounted: function(){
        this.locationStart();
        this.locationExp();
        this.totalSite();
    },
    methods: {
        totalSite:function () {
            if (this.user_id && this.token) {
            axios.get(this.host + '/introduce/', {
                    // 向后端传递JWT token的方法
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',
                })
                .then(response => {
                    console.log(response.data)
                    this.sophistication=response.data.sophistication
                    console.log(this.sophistication)
                })
                .catch(error => {

                    if (error.response.status==401 || error.response.status==403) {
                        location.href = '/person-loginsign.html?next=/person-account.html';
                    }
                });
        } else {
            location.href = '/person-loginsign.html?next=/person-account.html';
        }

        },
        locationStart:function () {
            if (this.user_id && this.token) {
            axios.get(this.host + '/user/', {
                    // 向后端传递JWT token的方法
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',
                })
                .then(response => {
                    // 加载用户数据
                    // console.log(response.data)
                    this.user=response.data
                    this.user_detail=response.data.detail;
                    this.user_id = response.data.id;
                    this.nick_name = response.data.nick_name;
                    this.mobile = response.data.mobile;
                    this.email = response.data.email;
                    this.username=response.data.username;
                    this.web_site = this.user_detail.web_site;
                    this.real_name = this.user_detail.real_name;
                    this.gender = this.user_detail.gender;
                    this.birthday = this.user_detail.birthday;
                    this.city = this.user_detail.city;
                    this.company = response.data.company.company;
                    this.professional = response.data.school.professional;
                    this.school_name = response.data.school.school_name;
                    this.profession = response.data.company.profession;
                    this.technology = this.user_detail.technology;
                    this.hometown = this.user_detail.hometown;
                    this.exp = this.user_detail.exp;
                    this.education = this.user_detail.education;
                    this.introduce = this.user_detail.introduce;
                    this.avatar_url = this.user_detail.avatar_url;

                })
                .catch(error => {

                    if (error.response.status==401 || error.response.status==403) {
                        location.href = '/person-loginsign.html?next=/person-account.html';
                    }
                });
        } else {
            location.href = '/person-loginsign.html?next=/person-account.html';
        }
        },
        locationExp:function () {
            if (this.user_id && this.token) {
            axios.get(this.host + '/exp_detail/', {
                    // 向后端传递JWT token的方法
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',
                })
                .then(response => {
                    let exps=  response.data;
                     for(var i=0 ;i<exps.length;i++){
                        exps[i].show = false;
                    }
                    this.array_exp=exps;
                    // console.log(response.data)
                })
                .catch(error => {

                    if (error.response.status==401 || error.response.status==403) {
                        location.href = '/person-loginsign.html?next=/person-account.html';
                    }
                });
        } else {
            location.href = '/person-loginsign.html?next=/person-account.html';
        }
        },
        locationEdu:function () {
            if (this.user_id && this.token) {
            axios.get(this.host + '/edu_detail/', {
                    // 向后端传递JWT token的方法
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',
                })
                .then(response => {
                    let edus=  response.data;
                     for(var i=0 ;i<edus.length;i++){
                        edus[i].show = false;
                    }
                    this.array_edu=edus;
                    // console.log(response.data)
                })
                .catch(error => {

                    if (error.response.status==401 || error.response.status==403) {
                        location.href = '/person-loginsign.html?next=/person-account.html';
                    }
                });
        } else {
            location.href = '/person-loginsign.html?next=/person-account.html';
        }
        },
        showCont: function (flag) {
            if (this.user_id && this.token) {
                if (flag == 0) {
                    vm.hide = false
                    axios.patch(this.host + '/accounts/'+this.user_id+'/', {
                            city: this.city
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {
                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {
                                console.log(error.response.data);
                            }
                        })
                } else if (flag == 1) {
                    vm.hide1 = false
                     axios.patch(this.host + '/school/'+this.user_id+'/', {
                         school_name:this.school_name,
                         professional:this.professional,
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            alert('保存成功')
                        })
                        .catch(error => {
                            console.log(response)
                            if (error.response.status == 400) {

                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {

                                console.log(error.response.data);
                            }
                        })
                } else if (flag == 2) {
                    vm.hide2 = false
                    axios.patch(this.host + '/company/'+this.user_id+'/', {
                        company: this.company,
                        profession:this.profession,
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            alert('保存成功')
                        })
                        .catch(error => {
                             alert('参数错误')
                            if (error.response.status == 400) {
                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {

                                console.log(error.response.data);
                            }
                        })
                } else if (flag == 3) {
                    vm.hide3 = false
                    axios.patch(this.host + '/accounts/'+this.user_id+"/", {
                           web_site: this.web_site
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {
                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {

                                console.log(error.response.data);
                            }
                        })
                }
            }

        },
        hidden:function (flag) {
            if (this.user_id && this.token) {
                if (flag == 0) {
                    vm.show = false
                    axios.patch(this.host + '/account/'+this.user_id+"/", {
                            nick_name: this.nick_name
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {
                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {
                                alert(this.token)
                                console.log(error.response.data);
                            }
                        })
                } else if (flag == 1) {
                    vm.show1 = false
                     axios.patch(this.host + '/accounts/'+this.user_id+'/', {
                            web_site: this.web_site
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            console.log(response.data)
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {

                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {
                                alert(this.token)
                                console.log(error.response.data);
                            }
                        })
                } else if (flag == 2) {
                    vm.show2 = false
                    axios.patch(this.host + '/account/'+this.user_id+'/', {
                            email: this.email
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            console.log(11111)
                            alert('保存成功')
                        })
                        .catch(error => {

                            if (error.response.status == 400) {
                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {
                                console.log(error.response.data);
                            }
                        })
                } else if (flag == 3) {
                    vm.show3 = false
                    axios.patch(this.host + '/account/'+this.user_id+'/', {
                           mobile: this.mobile
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {
                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {

                                console.log(error.response.data);
                            }
                        })
                } else if (flag == 4) {
                    vm.show4 = false
                    axios.patch(this.host + '/account/'+this.user_id+'/', {
                           password: this.password
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {
                                alert('密码输入错误')
                                if ('non_field_errors' in error.response.data) {

                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {
                                console.log(error.response.data);
                            }
                        })
                }
            }else{
                location.href = '/person-loginsign.html?next=/person-account.html';
            }

        },
        mytest:function (a) {
            if (this.user_id && this.token) {
                if(a==0){
                console.log("chenggong")
                    　vm.flag1=true
                     axios.patch(this.host + '/accounts/'+this.user_id+'/', {
                            introduce: this.introduce
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {

                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {
                                console.log(error.response.data);
                            }
                        })
                }else if(a==1){
                    　vm.flag2=true
                     axios.patch(this.host + '/edit/'+this.user_id+'/', {
                            avatar_url: this.avatar_url,
                            username: this.username,
                            birthday: this.birthday,
                            gender: this.num,
                            web_site: this.web_site,
                            mobile: this.mobile,
                            email: this.email,
                            city: this.city,
                            hometown: this.hometown,
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            this.totalSite()
                            console.log(response.data)
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {

                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {
                                console.log(error.response.data);
                            }
                        })
                }else if(a==2) {
                    vm.flag3 = true
                    axios.patch(this.host + '/accounts/' + this.user_id + '/', {
                            technology:this.technology
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials: true
                        })
                        .then(response => {
                            this.totalSite()
                            console.log(response.data)
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {

                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {
                                console.log(error.response.data);
                            }
                        })
                }else if(a==3){
                　
                }else if(a==4){
                    　vm.flag6=true
                     axios.post(this.host + '/work/'+this.user_id+'/', {
                            background:this.background,
                            edu_technology:this. edu_technology,
                            achievements:this.achievements
                        },
                        {
                            headers: {
                                'Authorization': 'JWT ' + this.token

                            },
                            responseType: 'json',
                            withCredentials:true
                        })
                        .then(response => {
                            console.log(response.data)
                            alert('保存成功')
                        })
                        .catch(error => {
                            if (error.response.status == 400) {

                                if ('non_field_errors' in error.response.data) {
                                    this.error_sms_code_message = error.response.data.non_field_errors[0];
                                } else {
                                    this.error_sms_code_message = '数据有误';
                                }
                                this.error_sms_code = true;
                            } else {
                                console.log(error.response.data);
                            }
                        })
                }
            }

        },
        showSlected:function (e) {

            this.technology=e.target.innerHTML

        },
        onclick:function (index,n) {

            if(n==0){
                this.array_exp[index].show = true;
            }else if(n==1){
                this.array_exp[index].show = false;
            }else {
                 vm.flag5=true
                 exp_id=this.array_exp[index].id
                 this.array_exp[index].show = false
                 axios.patch(this.host + '/exps/'+exp_id+"/", {
                        exp_company:this.exp_company,
                        exp_profession:this.exp_profession,
                        start_time:this.start_time,
                        end_time:this.end_time,
                        exp_technology:this.exp_technology,
                        work_city:this.work_city,
                        job_description:this.job_description
                    },
                    {
                        headers: {
                            'Authorization': 'JWT ' + this.token

                        },
                        responseType: 'json',
                        withCredentials:true
                    })
                    .then(response => {
                        this.totalSite()
                        let exps=  response.data;
                        for(var i=0 ;i<exps.length;i++){
                             exps[i].show = false;
                        }
                        this.array_exp=exps;
                        console.log(response.data)
                        alert('保存成功')

                    })
                    .catch(error => {
                        if (error.response.status == 400) {

                            if ('non_field_errors' in error.response.data) {
                                this.error_sms_code_message = error.response.data.non_field_errors[0];
                            } else {
                                this.error_sms_code_message = '数据有误';
                            }
                            this.error_sms_code = true;
                        } else {
                            console.log(error.response.data);
                        }
                    })
            }


            // alert('ok' + index)
            // this.sty=e.currentTarget.parentElement.parentElement.parentElement.nextElementSibling
            // // console.log(this.sty)
            // console.log(this.sty.getAttributeNode('style'))
            // console.log(this.sty.attr('style'))

        },
        createExp:function (index) {
             // exp_id=this.array_exp[index].id
             this.array_exp[index].show = false
             axios.post(this.host + '/exp/', {
                    exp_company:this.exp_company,
                    exp_profession:this.exp_profession,
                    start_time:this.start_time,
                    end_time:this.end_time,
                    exp_technology:this.exp_technology,
                    work_city:this.work_city,
                    job_description:this.job_description
                },
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token

                    },
                    responseType: 'json',
                    withCredentials:true
                })
                .then(response => {
                    this.totalSite()
                    let exps=  response.data;
                    for(var i=0 ;i<exps.length;i++){
                         exps[i].show = false;
                    }
                    this.array_exp=exps;
                    console.log(response.data)
                    alert('保存成功')

                })
                .catch(error => {
                    if (error.response.status == 400) {

                        if ('non_field_errors' in error.response.data) {
                            this.error_sms_code_message = error.response.data.non_field_errors[0];
                        } else {
                            this.error_sms_code_message = '数据有误';
                        }
                        this.error_sms_code = true;
                    } else {
                        console.log(error.response.data);
                    }
                })

        },
        LastShow:function (index) {
            // console.log(array.length)
            // console.log(this.index)

            let len =this.array_exp.length-1
            if(index==len){
                return "ture"

            }// getLength:function (array) {

        },
        expShow:function (index) {
            this.array_exp[index].show = true
            this.exp_flag=true

        },
        firstExp:function () {
            this.exp_flag1=false
            axios.post(this.host + '/exp/', {
                    exp_company:this.exp_company,
                    exp_profession:this.exp_profession,
                    start_time:this.start_time,
                    end_time:this.end_time,
                    exp_technology:this.exp_technology,
                    work_city:this.work_city,
                    job_description:this.job_description
                },
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token

                    },
                    responseType: 'json',
                    withCredentials:true
                })
                .then(response => {
                    let exps=  response.data;
                    for(var i=0 ;i<exps.length;i++){
                         exps[i].show = false;
                    }
                    this.array_exp=exps;
                    this.totalSite()
                    // console.log(response.data)
                    alert('保存成功')

                })
                .catch(error => {
                    if (error.response.status == 400) {

                        if ('non_field_errors' in error.response.data) {
                            this.error_sms_code_message = error.response.data.non_field_errors[0];
                        } else {
                            this.error_sms_code_message = '数据有误';
                        }
                        this.error_sms_code = true;
                    } else {
                        console.log(error.response.data);
                    }
                })

        },
        photo_png:function () {
            axios.patch(this.host + '/avatar/'+this.user_id+'/', {
                  avatar_url:this.avatar_url
                },
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token

                    },
                    responseType: 'json',
                    withCredentials:true
                })
                .then(response => {
                    let exps=  response.data;
                    for(var i=0 ;i<exps.length;i++){
                         exps[i].show = false;
                    }
                    this.array_exp=exps;
                    this.totalSite()
                    console.log(response.data)
                    alert('保存成功')

                })
                .catch(error => {
                    if (error.response.status == 400) {

                        if ('non_field_errors' in error.response.data) {
                            this.error_sms_code_message = error.response.data.non_field_errors[0];
                        } else {
                            this.error_sms_code_message = '数据有误';
                        }
                        this.error_sms_code = true;
                    } else {
                        console.log(error.response.data);
                    }
                })


        },
        getFile:function () {
            alert("ok")

        }
    }
    /**
     * Created by python on 19-4-12.
     */
})/**
 * Created by python on 19-4-18.
 */
